<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %><!-- 数据库操作需要 -->
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>         
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
PreparedStatement pstmt  =null;
Connection conn=null;
ResultSet rs = null;
try{
	Class.forName("org.h2.Driver");//加载驱动
} catch (ClassNotFoundException e) {
	e.printStackTrace();
	return;
}
try {
	conn = DriverManager.getConnection("jdbc:h2:d:/CompanyCar/temp/mydbtest", "sa", "");
	String sql1="SELECT * FROM car_maintenance WHERE license_plate = ?;";
	pstmt=conn.prepareStatement(sql1);
	pstmt.setString(1,request.getParameter("车牌号码"));
	System.out.println(sql1);
	rs = pstmt.executeQuery();
    if (!rs.next()) 
    {
        // 如果查询结果为空，则弹出提示框
        out.println("<script>alert('该车牌号不存在！');window.history.back();</script>");
    }
    else
    {
    	String sql="INSERT INTO car_maintenance (license_plate, maintenance_date, maintenance_mileage, next_maintenance_date, next_maintenance_mileage, maintenance_type, maintenance_cost, handler, register_person, register_date, last_modifier, last_modify_date, maintenance_item, maintenance_location, remark) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    	pstmt  = conn.prepareStatement(sql);
    	pstmt.setString(1,request.getParameter("车牌号码"));
    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    	java.util.Date date=null;
    	String dateString=request.getParameter("保养时间");
    	date= dateFormat.parse(dateString);
    	java.sql.Date maintenance_date = new java.sql.Date(date.getTime());
    	pstmt.setDate(2,maintenance_date);
    	pstmt.setFloat(3,Float.parseFloat(request.getParameter("保养里程")));
    	dateString = request.getParameter("下次保养时间");
    	date = dateFormat.parse(dateString);
    	java.sql.Date next_maintenance_date = new java.sql.Date(date.getTime());
    	pstmt.setDate(4,next_maintenance_date);
    	pstmt.setFloat(5,Float.parseFloat(request.getParameter("下次保养里程")));
    	pstmt.setString(6,request.getParameter("保养类型"));
    	pstmt.setFloat(7,Float.parseFloat(request.getParameter("本次保养费用")));
    	pstmt.setString(8,request.getParameter("经办人"));
    	pstmt.setString(9,request.getParameter("登记人"));
    	dateString = request.getParameter("登记时间");
    	date = dateFormat.parse(dateString);
    	java.sql.Date register_date = new java.sql.Date(date.getTime());
    	pstmt.setDate(10,register_date);
    	pstmt.setString(11,request.getParameter("最新一次修改人"));
    	dateString = request.getParameter("最新一次修改时间");
    	date = dateFormat.parse(dateString);
    	java.sql.Date last_modify_date = new java.sql.Date(date.getTime());
    	pstmt.setDate(12,last_modify_date);
    	pstmt.setString(13,request.getParameter("保养项目"));
    	pstmt.setString(14,request.getParameter("保养地点"));
    	pstmt.setString(15,request.getParameter("备注"));
    	System.out.println(sql);
    	//执行数据库插入，获取结果集
    	int result=pstmt.executeUpdate();
    	System.out.println(result);
    	response.sendRedirect("D-add_maintenance.jsp");
    }
	
	rs.close();
	//关闭表达式对象
	pstmt.close();			
	//关闭数据库				
	conn.close();
} catch (SQLException e) {
	e.printStackTrace();
} finally{
//防止数据库未正常关闭
try{
	if (conn!=null && (!conn.isClosed())){
		conn.close();
	}
}catch(SQLException e){
	e.printStackTrace();
}
}

%>


</body>
</html>